



/**
 *
 * WorkflowApi 工作流接口文件
 *
 * ---------------------------------------------
 *
 * API:
 *
 * WorkflowApi.newWorkflowApp(setting)
 *
 */
var WokflowApi = (function(){

    var logger = WorkflowUtil.newLogger("WorkflowApi");

    var WokflowApi = function(){

        //缓存工作流APP
        this._apps = {};

        //工作流应用序号
        this._appIndex = 0;
    }

    //API定义
    var apiDefs = {
      "newWorkflowApp" : "_newWorkflowApp"
    };
    for(var key in apiDefs){

      var functionName = apiDefs[key];
      if(key == functionName){
        logger.error("错误的接口定义， 方法名和实际执行函数相同了.");
        continue;
      }

      WokflowApi.prototype[key] = (function(functionName){
        return function(){
          if(!WokflowApi.prototype[functionName]){
            logger.error(functionName + "接口未定义");
          }else{
            //console.info("接口拦截设置...");
            WokflowApi.prototype[functionName].apply(this, arguments);
          }
        }
      })(functionName);
    }


    /**
     *
     * 初始化工作流
     *
     * @param setting 参数详细见 WorkflowApp 构造函数
     *
     * setting = {
     *    target : "#drawing"
     * }
     *
     * @returns WorkflowApp
     *
     */
    WokflowApi.prototype._newWorkflowApp = function(setting){

        var _target = setting.target;


        logger.log("新建工作流应用, target = '" + target + "'");


        if(!_target){
            throw new Error('参数错误, 参数缺少 "target"');
        }

        var workflowApp = this._apps[_target];
        if(workflowApp){
            //缓存中有
            workflowApp.reload(setting);
        }else{
            var _targetEle = WorkflowUtil.query(_target);
            if(!_targetEle){
                throw new Error('参数错误, 根据选择器"' + _target + '"找不到对应的dom元素.');
            }

            var _thisIndex = this._appIndex++;

            _targetEle.setAttribute("WORKFLOW_INDEX", _thisIndex);

            workflowApp = new WorkflowApp(setting);
        }

        this._apps[_target] = workflowApp;

        return workflowApp;
    }


    //返回实例
    return new WokflowApi();

})();
